home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / clang / cresid.zip / INTMAIN.C < prev    next >
Text File  |  1986-03-05  |  4KB  |  92 lines

  1. /****************************************************************************
  2. *        INTMAIN.C -- main routine for a C interrupt handler
  3. *
  4. *    This source file demonstrates how to make a routine resident. Notice
  5. *    that it calls int_init(), the interrupt initializtion function. 
  6. *    Another routine, inttime(), is the actual interrupt handler.
  7. *
  8. * ---------------------------------------------------------------------------
  9. *        How to Create an Interrupt-Driven C Routine
  10. *
  11. *    The theory behind interrupt driven  code is easy to understand. The
  12. *    first thing to remember is that when an interrupt occurs, YOU CAN NOT 
  13. *    BE SURE OF WHAT IS IN THE MACHINE REGISTERS. This is critical when
  14. *    hooking into hardware interrupts. Therefore, you must recreate your
  15. *    program environment every time your interrupt routine gets called.
  16. *
  17. *    Lattice C code relies on certain things to always be available.
  18. *    The stack, heap, and static areas must ALWAYS be availble to the C
  19. *    code. For this reason, you cannot just keep a C program resident and
  20. *    expect it work. This source, along with CINT.ASM, show you the hows
  21. *    and whys of setting up a C environment.
  22. *
  23. *    Step 1: Write your actual interrupt handler. It can do almost 
  24. *    anything, depending on what type of interrupt you are hooking into.
  25. *    (Example: A routine that hooks into interrupt 24H MUST NOT call any
  26. *    DOS services except for services 01 - 12H. Doing so will destory the
  27. *    DOS stack.)
  28. *    When you compile this source, do so in the small model. DO NOT use
  29. *    the STD I/O routines (printf, scanf, etc.). Actually, you can try,
  30. *    but then you should disregard Step 2.
  31. *
  32. *    Step 2: Recompile _MAIN.C in the S Model with the option -dTINY.
  33. *    What this does is create a new version of _main that does not open
  34. *    any of the STD I/O ports. (STDIN, STDOUT, STDERR, and in 3.00 STDPRT,
  35. *    and STDAUX.) Doing this will cut the size of the executable by at
  36. *    least 5K.
  37. *
  38. *    Step 3: Modify CINT.ASM to suit your needs. This part is up to you.
  39. *    You may need to do what I did and hook into two interrupts. My other
  40. *    handler was written in assembler, and it just reinstalled my critical
  41. *    error vector. (A nasty trick, but one that was required.)
  42. *
  43. *    Step 4: Link the .OBJ files. You will have to use the C.OBJ in the
  44. *    \LC\C directory. This allows us to EXE2BIN the resulting .EXE file.
  45. *    Include the required libraries (LC.LIB, and whatever), and ignore the
  46. *    "No stack" warning.
  47. *
  48. *    Step 5: EXE2BIN the .EXE file. EXE2BIN seems to have problems with
  49. *    programs over 32K in size. Hopefully, you won't be getting this big.
  50. *    Rename the resulting .BIN file to a .COM file.
  51. *
  52. *    Step 6: Run and/or debug the program, just like any other program.
  53. *    Just remeber to reboot the machine before you install your interrupt
  54. *    handler again.
  55. *
  56. *    That's about it. You should now, with a little work, be able to write
  57. *    your own "SideKick" type program, or anything that requires itself to
  58. *    be patched into an interrupt and resident in memory.
  59. *
  60. *    John Riley, Lattice Techincal Support
  61. *
  62. ****************************************************************************/
  63.  
  64. #include "dos.h"    /* Used for the REGS declaration */
  65.  
  66. extern unsigned _tsize;    /* This is defined in C.ASM. It contains the size of
  67.                the entire program in paragraphs */
  68.  
  69. main()
  70. {
  71.     union REGS out;
  72.     int_init();        /* Hook into the interrupt */
  73.     out.x.dx = _tsize;    /* Setup for the intdos call */
  74.     out.x.ax = 0x3100;    /* Terminate and stay resident call */
  75.     intdos(&out,&out);
  76. }
  77.  
  78.  
  79. /****************************************************************************
  80. *
  81. *    INTTIME -- Interrupt handler
  82. *
  83. *    This function can do anything it wants (within reason).
  84. *
  85. ****************************************************************************/
  86. inttime()
  87. {
  88.     cprintf("Got here!!!\n");
  89.     return(0);
  90. }
  91.  
  92.